package com.ttn.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ttn.business.mapper.CourseUnitMapper;
import com.ttn.business.service.CourseUnitService;
import com.ttn.business.service.GradeService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnit;
import com.ttn.common.core.entity.business.Grade;
import com.ttn.common.core.entity.business.converter.CourseUnitConverter;
import com.ttn.common.core.entity.business.vo.CourseUnitVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 题库管理(StudentRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-01-30 15:57:40
 */
@Slf4j
@Service("courseUnitService")
public class CourseUnitServiceImpl extends ServiceImpl<CourseUnitMapper, CourseUnit> implements CourseUnitService {
    @Resource
    private CourseUnitMapper courseUnitMapper;
    @Resource
    private GradeService gradeService;

    @Override
    public Result queryCourseUnitForGradePage(PageQuery pageQuery) {
        IPage<Grade> gradePage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        return Result.success(courseUnitMapper.queryCourseUnitForGradePage(gradePage));
    }

    @Override
    public Result queryCourseUnitById(Integer id) {
        CourseUnitVO courseUnitVO = new CourseUnitVO();
        CourseUnit courseUnit = this.getById(id);
        if (Objects.nonNull(courseUnit)) {
            Grade grade = gradeService.getById(courseUnit.getGrade());
            courseUnitVO = CourseUnitConverter.INSTANCE.CourseUnit2VO(courseUnit);
            if (Objects.nonNull(grade)) {
                courseUnitVO.setGradeName(grade.getGradeName());
            }
        }
        return Result.success(courseUnitVO);
    }

    @Override
    public Result serviceQueryCourseUnitNumberByGrade(Integer gradeId) {
        LambdaQueryWrapper<CourseUnit> courseUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitLambdaQueryWrapper.eq(CourseUnit::getGrade, gradeId);
        List<CourseUnit> courseUnitList = this.list(courseUnitLambdaQueryWrapper);
        if (courseUnitList.size() == 0) {
            return Result.success(0);
        }
        return Result.success(courseUnitList.size());
    }

    @Override
    public Result serviceQueryQuestionsNumberByUnitId(Integer unitId) {
//        LambdaQueryWrapper<CourseUnit> courseUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        courseUnitLambdaQueryWrapper.eq(CourseUnit::, unitId);
//        List<CourseUnit> courseUnitList = this.list(courseUnitLambdaQueryWrapper);
//        if (courseUnitList.size() == 0) {
//            return Result.success(0);
//        }
        return Result.success(courseUnitMapper.queryCourseUnitNumberByGrade(Lists.newArrayList(unitId)));
    }
}
